<?php

namespace App\Admin\Controller;

class System extends Authed {

    public function system() {
        
        if ($this->request->isPost()) {
            $posts = $this->request->getPost();

            while ($key = key($posts)) {
                if (strpos($key, 'value') == 0) {
                    QF()->System()->setOne($key, current($posts));
                }
                next($posts);
            }

            // 清理缓存
            if (!cache('system', 'dbcfg', null, 0, true)) {
                fail();
            }

            succ('系统设置已修改');
        }

        // 列出所有系统设置
        $rows = QF()->System()->getAll();

        $this->assign('rows', $rows);
    }

    public function config() {
        if ($this->request->isPost()) {
            $posts = $this->request->getPost();

            while ($key = key($posts)) {
                if (strpos($key, 'value') == 0) {
                    QF()->Config()->setOne($key, current($posts));
                }
                next($posts);
            }

            succ('系统参数已修改');
        }

        // 列出所有参数设置
        $rows = QF()->Config()->getAll();
        $this->assign('rows', $rows);
    }

    public function password() {

        $uid = \Auth::getAuthedUid(MODULE_NAME);

        if ($this->request->isPost()) {

            $old = $this->request->getPost('old', '');
            $new = $this->request->getPost('new', '');
            $confirm = $this->request->getPost('confirm', '');

            if (trim($old) == '') {
                fail('请输入旧密码');
            } elseif (trim($new) == '') {
                fail('请输入新密码');
            } elseif ($new != $confirm) {
                fail('两次输入的密码不一致');
            }

            $result = QF()->Admin()->updatePassword($uid, $old, $new);
            if ($result) {
                succ('', '', 2);
            } else {
                fail('密码更新失败，旧密码是否正确？');
            }
        }
    }

}
